import { CoolController, BaseController } from '@cool-midway/core';
import { UserInfoEntity } from '../../entity/info';
import { BaseSysUserEntity } from '../../../base/entity/sys/user';
import { OrderShopEntity } from '../../../order/entity/shop';
import { AppointmentInfoEntity } from '../../../appointment/entity/info';
import { UserInfoService } from '../../service/info';
import { Inject } from '@midwayjs/core';

/**
 * 用户信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: UserInfoEntity,
  service: UserInfoService,
  pageQueryOp: {
    fieldEq: ['a.registAdminId', 'a.shopId'],
    keyWordLikeFields: ['a.nickName', 'a.phone'],
    select: [
      'a.*',
      'b.name as adminName',
      'c.name as shopName',
      'd.appointmentTime as lastAppointTime',
      'd.shopId as lastAppointShopId',
    ],
    join: [
      {
        entity: BaseSysUserEntity,
        alias: 'b',
        condition: 'a.registAdminId = b.id',
        type: 'leftJoin',
      },
      {
        entity: OrderShopEntity,
        alias: 'c',
        condition: 'a.shopId = c.id',
        type: 'leftJoin',
      },
      {
        entity: AppointmentInfoEntity,
        alias: 'd',
        condition: 'a.lastAppointId = d.id',
        type: 'leftJoin',
      }
    ],
    where: async ctx => {
      const { shopId } = ctx.request.body;
      const where = [];
      if (shopId) {
        where.push([
          'a.shopId = :shopId',
          { shopId }
        ]);
      }
      return where;
    }
  },
})
export class AdminUserInfoController extends BaseController {
  @Inject()
  userInfoService: UserInfoService;
}
